package com.zjpavt.client.page;

import com.zjpavt.client.extenddevice.AbstractExtendDevice;
import com.zjpavt.client.extenddevice.IVoltageAndCurrentDetection;
import com.zjpavt.client.hal.ExtendDeviceManager;
import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.hal.VoltageAndCurrentManager;
import com.zjpavt.client.page.abstractpage.AbstractBasePage;
import com.zjpavt.client.page.abstractpage.AbstractEditablePage;
import com.zjpavt.client.page.basepage.BaseEditablePage;
import com.zjpavt.client.page.basepage.BaseSonPage;
import com.zjpavt.client.page.editableunit.EditUnit;
import com.zjpavt.client.page.editableunit.EditUnitNum;
import com.zjpavt.client.page.intefacepage.EditablePage;
import com.zjpavt.client.page.intefacepage.ParentPage;
import com.zjpavt.client.page.intefacepage.SonPage;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.util.NumberUtil;
import com.zjpavt.client.util.PropertiesUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyc
 * 电流电压检测模块配置页面
 */
public class ElecConfigPage extends AbstractEditablePage implements EditablePage,SonPage {
    private int elecChannelNum;
    private boolean isUseCircuit;
    private SonPage sonPage;
    private EditablePage editablePage;
    public static final String ELEC_CHANNEL_NUM = "elecChannelNum";
    public static final String ELEC_CHANNEL_IS_TESTING = "elec_Channel_Is_Testing";
    public static final String DEFAULT_ELEC_CHANNEL_IS_TESTING = "default_elec_Channel_Is_Testing";

    public ElecConfigPage(int pageNum, ParentPage parentPage, long mRefreshPeriod) {
        super(pageNum, mRefreshPeriod);
        sonPage = new BaseSonPage(getPageNum(), parentPage,0, this);
        editablePage = new BaseEditablePage(getPageNum(), 0,this);
//      初始化文件中的回路设置
        try {
            String elecNumInFile = PropertiesUtil.getPropertiesDefault(ELEC_CHANNEL_NUM,"12");
            this.elecChannelNum = Integer.valueOf(elecNumInFile);
            if (elecChannelNum != 12) {
                PropertiesUtil.setPropertyOrReplaceDefault(ELEC_CHANNEL_NUM,elecNumInFile);
            }
            String default_elec_Channel_Is_Testing = PropertiesUtil.getPropertiesDefault(DEFAULT_ELEC_CHANNEL_IS_TESTING);
            if(null == default_elec_Channel_Is_Testing || "".equals(default_elec_Channel_Is_Testing)) {
                PropertiesUtil.setPropertyOrReplaceDefault(DEFAULT_ELEC_CHANNEL_IS_TESTING,"000000000000");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 编辑的时候 缓存可编辑单元中的值
     * @return
     */
    public int getElecChannelNumWhenEdit() {
        return Integer.parseInt(getEditUnit(0).getText());
    }

    @Override
    public void refreshPage() {
        List<ScreenLine> result = new ArrayList<>();
        result.add(new ScreenLine.ScreenLineBuilder().text("Elec").pageNum(getPageNum()).build());
        result.add(new ScreenLine.ScreenLineBuilder().text("channelNum:" + NumberUtil.getFormat00(elecChannelNum) ).build());
        result.add(new ScreenLine.ScreenLineBuilder().text("").build());
        result.add(new ScreenLine.ScreenLineBuilder().text("").build());
        LedScreen.getInstance().setText(result);
    }

    /********************************************************
     * sonpage
     * @return
     */
    @Override
    public ParentPage getParentPage() {
        return sonPage.getParentPage();
    }


    @Override
    public void enterSonPage() {
        refreshPage();
    }

    @Override
    public void exitSonPage() {
        if (isInEditMode()) {
            throw new IllegalArgumentException();
        }
        sonPage.exitSonPage();
    }

    @Override
    public AbstractBasePage setParentPage(ParentPage basePageImpl) {
        sonPage.setParentPage(basePageImpl);
        return this;
    }

    @Override
    public void close() {

    }

    /*************************************************
     * editpage
     */
    @Override
    public void addEditUnit(EditUnit e) {
        editablePage.addEditUnit(e);
    }

    @Override
    public void removeEditUnit(EditUnit e) {
        editablePage.removeEditUnit(e);
    }

    @Override
    public void clearEditUnit() {
        editablePage.clearEditUnit();
    }

    @Override
    public EditUnit getEditUnit(int index) {
        return editablePage.getEditUnit(index);
    }

    @Override
    public int getCurrBlinkIndex() {
        return editablePage.getCurrBlinkIndex();
    }

    @Override
    public void exitEditMode() {
        editablePage.exitEditMode();
    }

    @Override
    public boolean isInEditMode() {
        return editablePage.isInEditMode();
    }

    @Override
    public int getEditUnitSize() {
        return editablePage.getEditUnitSize();
    }

    @Override
    public void enterEditMode() {
        this.editablePage.enterEditMode();
    }

    @Override
    public void setDefaultVal() {
        getEditUnit(0).setCurrentVal(elecChannelNum);
    }

    @Override
    public void initEditUnits() {
        addEditUnit(new EditUnitNum(2, 12, 13, 12, 99,1));
    }

    @Override
    public void saveVal() {
        try {
            this.elecChannelNum = getElecChannelNumWhenEdit();
            PropertiesUtil.setPropertyOrReplaceDefault(ELEC_CHANNEL_NUM, String.valueOf(elecChannelNum));
            VoltageAndCurrentManager.getInstance().getVoltageAndCurrentDetectionList().clear();
            //VoltageAndCurrentManager.getInstance().setvLoopNum(elecChannelNum);
            Map<Integer, IVoltageAndCurrentDetection> voltageAndCurrentExtendDevicetempMap = new HashMap<>();
            for(IVoltageAndCurrentDetection voltageAndCurrentDetection : ExtendDeviceManager.getInstance().getVoltageAndCurrentExtendDeviceMap().values()) {
                // ExtendDeviceManager.getInstance().getVoltageAndCurrentExtendDeviceMap().remove(voltageAndCurrentDetection.getExtendNum());
                AbstractExtendDevice extendDevice = ExtendDeviceManager.getInstance().findExtendDevice(voltageAndCurrentDetection.getExtendNum(),true);
                // ExtendDeviceManager.getInstance().getVoltageAndCurrentExtendDeviceMap().put(voltageAndCurrentDetection.getExtendNum(), (IVoltageAndCurrentDetection) extendDevice);
                VoltageAndCurrentManager.getInstance().getVoltageAndCurrentDetectionList().add((IVoltageAndCurrentDetection) extendDevice);
                voltageAndCurrentExtendDevicetempMap.put(voltageAndCurrentDetection.getExtendNum(), (IVoltageAndCurrentDetection) extendDevice);
            }
            ExtendDeviceManager.getInstance().getVoltageAndCurrentExtendDeviceMap().clear();
            ExtendDeviceManager.getInstance().getVoltageAndCurrentExtendDeviceMap().putAll(voltageAndCurrentExtendDevicetempMap);
            VoltageAndCurrentManager.getInstance().setvLoopNum(elecChannelNum);

            int num = elecChannelNum / (LedScreen.SCREEN_HEIGHT - 1);
            //每页三项，计算需要的内嵌页数
            int maxInnerPageNum = elecChannelNum % (LedScreen.SCREEN_HEIGHT - 1) == 0 ? num : num + 1;
            // VoltageAndCurrentManager.getInstance().setMaxInnerPageNum(maxInnerPageNum);
            if(elecChannelNum > 0) {
                StringBuffer  elec_Channel_Is_Testing_num =  new StringBuffer();
                for(int i = 0; i<elecChannelNum ; i++) {
                    elec_Channel_Is_Testing_num.append("0");
                }
                PropertiesUtil.setPropertyOrReplaceDefault(ELEC_CHANNEL_IS_TESTING, elec_Channel_Is_Testing_num.toString());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void init() {
        initEditUnits();
    }

    /******************************************************
     * basePage
     */
    @Override
    public void onKeyDown() {
        if (isInEditMode()) {
            editablePage.onKeyDown();
        } else {
            super.onKeyDown();
        }
    }

    @Override
    public void onKeyUp() {
        if (isInEditMode()) {
            editablePage.onKeyUp();
        } else {
            super.onKeyUp();
        }
    }

    @Override
    public void onKeySet() {
        if (isInEditMode()) {
            editablePage.onKeySet();
        } else {
            enterEditMode();
        }
    }

    /**
     * 在编辑模式下 确认时 退出编辑模式
     * 如果不在编辑下 退出当前的页面
     */
    @Override
    public void onKeyEnt() {
        if (isInEditMode()) {
            exitEditMode();
        } else {
            exitSonPage();
        }
    }
}
